package com.ruyuan.eshop.diplomacy.manager.custom.db;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Sets;
import com.ruyuan.eshop.diplomacy.domain.entity.SkuInfoDO;
import com.ruyuan.eshop.common.enums.DelFlagEnum;
import com.ruyuan.eshop.diplomacy.manager.common.db.AbstractRedisStringDatabase;
import com.ruyuan.eshop.diplomacy.manager.common.cache.RedisStringCache;
import com.ruyuan.eshop.diplomacy.mapper.SkuInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

/**
 * sku综合信息
 *
 * @author zhonghuashishan
 */
@Slf4j
@Service("skuCollectStringDatabase")
public class SkuCollectStringDatabase extends AbstractRedisStringDatabase<SkuInfoDO> {

    @Resource
    private SkuInfoMapper skuInfoMapper;

    /**
     * 表名
     */
    private static final String TABLE_NAME = "SKU_INFO";

    /**
     * 商品ID
     */
    public static final String SKU_ID = "skuId";

    /**
     * 数据源单表
     *
     * @return java.lang.String
     * @author zhonghuashishan
     */
    @Override
    public String getTableName() {
        return TABLE_NAME;
    }

    /**
     * 数据源全量表
     *
     * @param
     * @return java.util.Set<java.lang.String>
     * @author zhonghuashishan
     */
    @Override
    public Set<String> getTableNameSet() {
        return Sets.newHashSet(TABLE_NAME);
    }

    /**
     * 获取表数据
     *
     * @param tableFieldsMap    key 数据库字段，value 字段值
     * @param queryType         查询类型，多数据查询时区分
     * @return java.util.Optional<com.ruyuan.eshop.diplomacy.domain.entity.SkuInfoDO>
     * @author zhonghuashishan
     */
    @Override
    public Optional<SkuInfoDO> getTableData(Map<String, Object> tableFieldsMap, String queryType) {
        if (!tableFieldsMap.containsKey(SKU_ID)) {
            throw new IllegalArgumentException();
        }

        // 商品基本信息
        String skuId = (String) tableFieldsMap.get(SKU_ID);
        QueryWrapper<SkuInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("SKU_ID", skuId);
        SkuInfoDO skuInfo = skuInfoMapper.selectOne(queryWrapper);
        return null == skuInfo ? Optional.empty() : Optional.of(skuInfo);
    }

    /**
     * 获取表集合数据
     *
     * @param tableFieldsMap
     * @param queryType
     * @return java.util.Optional<java.util.List < T>>
     * @author zhonghuashishan
     */
    @Override
    public Optional<List<SkuInfoDO>> listTableData(Map<String, Object> tableFieldsMap, String queryType) {
        return Optional.empty();
    }

    /**
     * 根据ID批量获取数据
     *
     * @param idSet         表主键ID集合
     * @param queryType     查询类型，多数据查询时区分
     * @return java.util.Optional<com.ruyuan.eshop.diplomacy.manager.common.source.RedisStringSource<com.ruyuan.eshop.diplomacy.domain.entity.SkuInfoDO>>
     * @author zhonghuashishan
     */
    @Override
    public Optional<RedisStringCache<SkuInfoDO>> listTableDataByIdSet(Set<Integer> idSet, String queryType) {
        QueryWrapper<SkuInfoDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("ID", idSet);
        List<SkuInfoDO> skuInfos = skuInfoMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(skuInfos)) {
            return Optional.empty();
        }
        RedisStringCache<SkuInfoDO> redisStringCache = new RedisStringCache<>();
        List<SkuInfoDO> addList = skuInfos.stream().filter(doBase -> doBase.getDelFlag().equals(DelFlagEnum.EFFECTIVE.getCode())).collect(toList());
        Set<String> deleteSet = skuInfos.stream().filter(doBase -> doBase.getDelFlag().equals(DelFlagEnum.DISABLED.getCode())).map(doBase -> String.valueOf(doBase.getSkuId())).collect(toSet());
        redisStringCache.setAddList(addList);
        redisStringCache.setDeleteSet(deleteSet);
        return Optional.of(redisStringCache);
    }

    @Override
    public Optional<RedisStringCache<SkuInfoDO>> listTableDataByIdSet(List<Long> idSet, String queryType) {
        return Optional.empty();
    }

}
